/*
 * File:   main.cpp
 * Author: song
 *
 * Created on 2010年10月28日, 下午12:23
 */

#include <QtGui/QApplication>
#include "Data.h"
#include "Locate.h"
#include "Track.h"
#include "Control.h"
#include "PreProcess.h"
#include "Calculate.h"
#include<omp.h>

extern int numFrame;
extern int hdims;
extern int minT;



//启动摄像头录制模块
void StartCam(int lCamNum, int rCamNum)
{

    ofstream fout;
    fout.open("calSpeed.txt");
    
    Camera cam0;
    Camera cam1;

    int sumTime = 0;
    //图像预处理类
    PreProcess myPrePro0(&cam0), myPrePro1(&cam1);
    //目标检测类
    Locate myLoc0(&cam0), myLoc1(&cam1);
    //目标跟踪类
    Track myTrack0(&cam0), myTrack1(&cam1);
    //控制类
    Control myCon;

    if(myCon.OpenCam(cam0, lCamNum) == false || myCon.OpenCam(cam1, rCamNum) == false)
        return;

    //计算类
    Calculate myCal(&cam1, myTrack0.V, myTrack1.V, 10);
        double time;
        clock_t start, end;

    do{
        start = clock();

        //判断是否获取成功
        if(!cam0.pFrame){
            cout<<"图像获取失败"<<endl;
            return ;
        }
//        if(numFrame == 52){
//            cvShowImage("bk", cam0.pBkImg);
//        }
        myPrePro0.Process(cam0.pFrame, &cam0);
        myPrePro1.Process(cam1.pFrame, &cam1);
        ///////////////调试代码//////////////////////////////////
        cvConvert(cam0.pBkMat, cam0.pBkImg);
        cvConvert(cam1.pBkMat, cam1.pBkImg);
        //cvConvert(pFrMat, pFrImg);
        //cvConvert(pGrayMat, pGray);


        if(numFrame > 51)
        {
            myLoc0.scan(cam0.pFrMat);
            myLoc0.Draw(cam0.pFrImg, CV_RGB(255,255,255));
            myLoc1.scan(cam1.pFrMat);
            myLoc1.Draw(cam1.pFrImg, CV_RGB(255,255,255));
//                cvWaitKey(0);

            if(numFrame == 52)
            {
                myTrack0.Init();
                myTrack1.Init();
            }

            myTrack0.Run(myLoc0.V);
            myTrack1.Run(myLoc1.V);
        }

        cvCopy(cam0.pFrame, cam0.pPre);
        cvCopy(cam1.pFrame, cam1.pPre);

        if(numFrame > 51)
        {
             //if(numFrame >= 111) cvWaitKey(0);

            myTrack0.Draw(cam0.pFrame, CV_RGB(255,0,0));
            //myTrack1.Draw(cam1.pFrame, CV_RGB(255,0,0));

            //加入参数计算部分
//            myCal.Match(myTrack0.V, myTrack1.V);
//            myCal.CalZDis();
            cvShowImage("left", cam0.pFrame);
            myCal.Cal();

            //计算模块对图像进行匹配后再输出编号
            myCal.Draw(cam1.pFrame, CV_RGB(255,0,0));

            cvShowImage("right", cam1.pFrame);
        }

        cvShowImage("bk", cam0.pBkImg);
        cvShowImage("fr", cam0.pFrImg);

        ///////////////////////////////////////////////////////
        end = clock();
        time = (double)(end - start)/CLOCKS_PER_SEC * 1000;
        cout<<"time:"<<time<<endl;
//        time = 60 - time;
//        if(time <= 0) time = 1;
        //if(time < 60)
            cvWaitKey(10);
        //////////测试代码///////////////////
        sumTime += end - start;
        if(sumTime > CLOCKS_PER_SEC/2){
            sumTime = 0;
            vector<speedNode> tmp = myCal.GetSpeed();
            for(int i = 0; i < tmp.size(); i++) {
                fout<<"物体id： "<<tmp[i].id
                        <<" xSpeed: "<<tmp[i].Vx/100
                        <<" ySpeed: "<<tmp[i].Vy/100
                        <<" zSpeed: "<<tmp[i].Vz/100
                        <<endl;
            }
            fout<<endl;
        }
        ///////////////////////////////////
        cam0.pFrame = cvQueryFrame(cam0.capture) ;
        cam1.pFrame = cvQueryFrame(cam1.capture);

        //帧计数
        numFrame++;
        }
        while(cam0.pFrame && cam1.pFrame);
    
}

//启动avi文件读取模块
void StartAvi(char* path)
{
    int sumTime = 0;
    ofstream fout;
    fout.open("calSpeed.txt");

    Camera cam0;
    Camera cam1;

    //图像预处理类
    PreProcess myPrePro0(&cam0), myPrePro1(&cam1);
    //目标检测类
    Locate myLoc0(&cam0), myLoc1(&cam1);
    //目标跟踪类
    Track myTrack0(&cam0), myTrack1(&cam1);
    //控制类
    Control myCon;

    if(myCon.OpenAvi(cam0, "/home/song/Lout17.avi") == false || myCon.OpenAvi(cam1, "/home/song/Rout17.avi") == false)
        return;

    //计算类
    Calculate myCal(&cam1, myTrack0.V, myTrack1.V, 10);
        double time;
        clock_t start, end;

    do{
        start = clock();

        //判断是否获取成功
        if(!cam0.pFrame){
            cout<<"图像获取失败"<<endl;
            return ;
        }
//        if(numFrame == 52){
//            cvShowImage("bk", cam0.pBkImg);
//        }
        myPrePro0.Process(cam0.pFrame, &cam0);
        myPrePro1.Process(cam1.pFrame, &cam1);
        ///////////////调试代码//////////////////////////////////
        cvConvert(cam0.pBkMat, cam0.pBkImg);
        cvConvert(cam1.pBkMat, cam1.pBkImg);
        //cvConvert(pFrMat, pFrImg);
        //cvConvert(pGrayMat, pGray);
        cout<<"Frame: "<<numFrame<<endl;
        ///////////////调试代码//////////////////////////////////

        if(numFrame > 51)
        {
            myLoc0.scan(cam0.pFrMat);
            myLoc0.Draw(cam0.pFrImg, CV_RGB(255,255,255));
            myLoc1.scan(cam1.pFrMat);
            myLoc1.Draw(cam1.pFrImg, CV_RGB(255,255,255));
//                cvWaitKey(0);

            if(numFrame == 52)
            {
                myTrack0.Init();
                myTrack1.Init();
            }
            ////////////////////////调试代码////////////////////////////////
             //if(numFrame >= 128) cvWaitKey(0);
            ////////////////////////调试代码////////////////////////////////

            myTrack0.Run(myLoc0.V);
            myTrack1.Run(myLoc1.V);
        }

        //cvCopy(cam0.pFrame, cam0.pPre);
        //cvCopy(cam1.pFrame, cam1.pPre);
        


        if(numFrame > 51)
        {
             
            myTrack0.Draw(cam0.pFrame, CV_RGB(255,0,0));
            //myTrack1.Draw(cam1.pFrame, CV_RGB(255,0,0));
            
            //加入参数计算部分
//            myCal.Match(myTrack0.V, myTrack1.V);
//            myCal.CalZDis();
            cvShowImage("left", cam0.pFrame);
            myCal.Cal();

            //计算模块对图像进行匹配后再输出编号
            myCal.Draw(cam1.pFrame, CV_RGB(255,0,0));
            
            cvShowImage("right", cam1.pFrame);
        }

        cvShowImage("bk", cam0.pBkImg);
        cvShowImage("fr", cam1.pFrImg);

        ///////////////////////////////////////////////////////
        end = clock();
        time = (double)(end - start)/CLOCKS_PER_SEC * 1000;
        cout<<"time:"<<time<<endl;
//        time = 60 - time;
//        if(time <= 0) time = 1;
        //if(time < 60)
            cvWaitKey(10);
        //////////测试代码///////////////////
        sumTime += end - start;
        if(sumTime > CLOCKS_PER_SEC/2){
            sumTime = 0;
            vector<speedNode> tmp = myCal.GetSpeed();
            for(int i = 0; i < tmp.size(); i++) {
                fout<<"物体id： "<<tmp[i].id
                        <<" xSpeed: "<<tmp[i].Vx/100
                        <<" ySpeed: "<<tmp[i].Vy/100
                        <<" zSpeed: "<<tmp[i].Vz/100
                        <<endl;
            }
            fout<<endl;
        }
        ///////////////////////////////////
        cam0.pFrame = cvQueryFrame(cam0.capture) ;
        cam1.pFrame = cvQueryFrame(cam1.capture);
        
        //帧计数
        numFrame++;
    }
    while(cam0.pFrame && cam1.pFrame);
    
}


int main(int argc, char *argv[]) {
    // initialize resources, if needed
    // Q_INIT_RESOURCE(resfile);

    //QApplication app(argc, argv);
//    omp_set_dynamic(0);
//    omp_set_nested(1);

    //测试是否开启openmp选项
//int i = 0;
//
//#pragma omp parallel num_threads(3)
//{
//    #pragma omp parallel num_threads(2) shared(i)
//    {
//        cout<<"i:"<<i++<<endl;
//    }
//}
//
//int j = 2;
    //获取当前进程所在绝对路径
//    char link[100], path[100];
//    sprintf(link, "/proc/%d/exe", getpid());
//    readlink(link, path, sizeof(path));
//    printf("%s\n.", path);
    StartAvi("/home/song/桌面/test/test02.avi");
    //StartCam(2, 1);
    // create and show your widgets here

    return 0;
}
